package net.qunzhi.modules.cms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.qunzhi.common.exception.RRException;
import net.qunzhi.common.utils.BaseQuery;
import net.qunzhi.common.annotation.SysLog;
import net.qunzhi.common.utils.ShiroUtils;
import net.qunzhi.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.qunzhi.modules.cms.entity.TbCumulativeFlowEntity;
import net.qunzhi.modules.cms.service.TbCumulativeFlowService;
import net.qunzhi.common.utils.PageUtils;
import net.qunzhi.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 无流量计的累计数据
 *
 * @author zjr
 * @date 2021-12-01 16:57:51
 */
@Api(tags = "无流量计的累计数据相关")
@RestController
@RequestMapping("cms/tbCumulativeFlow")
public class TbCumulativeFlowController {
    @Autowired
    private TbCumulativeFlowService tbCumulativeFlowService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    @RequiresPermissions("cms:tbCumulativeFlow:list")
    public R list(BaseQuery query){
        PageUtils page = tbCumulativeFlowService.queryPage(query);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("cms:tbCumulativeFlow:info")
    public R info(@PathVariable("id") Long id){
		TbCumulativeFlowEntity tbCumulativeFlow = tbCumulativeFlowService.getById(id);

        return R.ok().put("tbCumulativeFlow", tbCumulativeFlow);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @SysLog("无流量计的累计数据保存")
    @PostMapping("/save")
    @RequiresPermissions("cms:tbCumulativeFlow:save")
    public R save(@RequestBody TbCumulativeFlowEntity tbCumulativeFlow){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        tbCumulativeFlow.setCreateUserId(userEntity.getUserId()).setCreateUserName(userEntity.getUsername());
		tbCumulativeFlowService.save(tbCumulativeFlow);

        return R.ok();
    }

    @ApiOperation("批量保存")
    @SysLog("无流量计的累计数据批量保存")
    @PostMapping("/saveBatch")
    @RequiresPermissions("cms:tbCumulativeFlow:save")
    public R save(@RequestBody List<TbCumulativeFlowEntity> form){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        var list = form.stream().filter(entity -> null != entity.getFlow()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            throw new RRException("请输入累计流量值");
        }
        list.forEach(entity -> {
            entity.setCreateUserId(userEntity.getUserId()).setCreateUserName(userEntity.getUsername());
        });
		tbCumulativeFlowService.saveBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @SysLog("无流量计的累计数据修改")
    @PostMapping("/update")
    @RequiresPermissions("cms:tbCumulativeFlow:update")
    public R update(@RequestBody TbCumulativeFlowEntity tbCumulativeFlow){
		tbCumulativeFlowService.updateById(tbCumulativeFlow);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @SysLog("无流量计的累计数据删除")
    @PostMapping("/delete")
    @RequiresPermissions("cms:tbCumulativeFlow:delete")
    public R delete(@RequestBody Long[] ids){
		tbCumulativeFlowService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
